//
//  HROriginWeiBoView.swift
//  weiBo
//
//  Created by helong on 16/7/13.
//  Copyright © 2016年 helong. All rights reserved.
//

/// 这是cell的自最上方的视图,自己发的微博的视图
import UIKit
import SDWebImage
import SnapKit

//  控件的间距
let Margin: CGFloat = 10


class HROriginWeiBoView: UIView {
    //  记录约束
    var originWeiBoViewBottonConstraint: Constraint?
    //  定义属性然后从cell中接收传递过来数据视图中的控件赋值
    var homePageStatusViewModel: HRHomePageStatusViewModel?{
        didSet{
            if let imageUrl = homePageStatusViewModel?.homePageModel!.user?.profile_image_url {
                //  用户头像
                profile_image.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            //  用户昵称
            screen_name.text = homePageStatusViewModel?.homePageModel!.user?.screen_name
            //  发微博时间
            created_at.text = homePageStatusViewModel?.creat_time ?? ""
            //  微博来源
            source.text = homePageStatusViewModel?.weiBoResouce
            //  微博的内容
            text.attributedText = homePageStatusViewModel?.originWeiboAttributeString
            //  微博会员等级
            verified_type.image = homePageStatusViewModel?.mbrankeImage
            //  认证等级
            mbrank.image = homePageStatusViewModel?.verifiedImage
            
            
            //  卸载约束
            originWeiBoViewBottonConstraint?.uninstall()
            //  给转发微博配图赋值
            if let count = homePageStatusViewModel?.homePageModel?.pic_urls?.count where count > 0 {// 如果数组里面有值
                //  传递数据
                originPicture.pic_urls = homePageStatusViewModel?.homePageModel?.pic_urls
                //  显示配图视图
                originPicture.hidden = false
                //  根据图片张数更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originWeiBoViewBottonConstraint = make.bottom.equalTo(originPicture).offset(Margin).constraint
                })
            }else{//    如果数组里面没有值,
                //  隐藏配图视图
                originPicture.hidden = true
                //  根据图片张数更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originWeiBoViewBottonConstraint = make.bottom.equalTo(text).offset(Margin).constraint
                })
            }

        }
    }
    
     // MARK: - 懒加载控件
    // 懒加载头像图片框
    private lazy var profile_image: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  懒加载昵称
    private lazy var screen_name: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //  懒加载等级图片框
    private lazy var verified_type: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  懒加载认证等级图片框
    private lazy var mbrank: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  懒加载时间
     private lazy var created_at: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 13)
    //  懒加载微博来源
     private lazy var source: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 13)
    //  懒加载微博内容
    private lazy var text: UILabel = {
        let lbl = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HomePageFontSize)
        lbl.numberOfLines = 0
        return lbl
    }()
    //  懒加载原创微博配图
    private lazy var originPicture: HRRetweetCollectionView = {
        let collectionView = HRRetweetCollectionView()
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = randomColor()
        printLog()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     // MARK: - 设置子控件,添加约束
    private func setupUI(){
        addSubview(profile_image)
        addSubview(screen_name)
        addSubview(verified_type)
        addSubview(mbrank)
        addSubview(created_at)
        addSubview(source)
        addSubview(text)
        addSubview(originPicture)
        
        profile_image.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(Margin)
            make.leading.equalTo(self).offset(Margin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        screen_name.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(profile_image.snp_trailing).offset(Margin)
            make.top.equalTo(profile_image)
        }
        verified_type.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screen_name.snp_trailing).offset(Margin)
            make.top.equalTo(screen_name)
        }
        mbrank.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(profile_image.snp_trailing)
            make.centerY.equalTo(profile_image.snp_bottom)
        }
        created_at.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(profile_image.snp_trailing).offset(Margin)
            make.bottom.equalTo(profile_image.snp_bottom)
        }
        source.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(created_at.snp_trailing).offset(Margin)
            make.bottom.equalTo(created_at)
        }
        text.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(Margin)
            make.top.equalTo(created_at.snp_bottom).offset(Margin)
            make.trailing.equalTo(self).offset(-Margin)
        }
        //  原创微博配图约束
        originPicture.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(text.snp_bottom).offset(Margin)
            make.leading.equalTo(text)

        }
        //  非常重要的约束:原创微博的视图的底部约束等于微博内容的底部+间距
        self.snp_makeConstraints { (make) -> Void in
           self.originWeiBoViewBottonConstraint = make.bottom.equalTo(originPicture).offset(Margin).constraint
        }
        
    }

}
